package com.gg.user.gguser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gg.header.getHeaderUser;
import com.gg.user.gguser.dao.GgInvitationCodeDaoMapper;
import com.gg.user.gguser.service.GgInvitationCodeServe;
import com.gg.vo.return_format;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @author yangxinlei
 * @date 2020/4/24
 */
@Service
public class GgInvitationCodeServiceImpl implements GgInvitationCodeServe {

    @Autowired
    private GgInvitationCodeDaoMapper ggInvitationCodeDaoMapper;

    @Override
    public Object addInvitationCode(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            String cookiceUser = getHeaderUser.getHader(response,request,"gg");
            String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
            String ggInvitationCode = UUID.randomUUID().toString();
            ggInvitationCodeDaoMapper.addInvitationCode(ggIdUser, ggInvitationCode);
            return return_format.return_format_All(0,ggInvitationCode,"");
        }catch (Exception e){
            return return_format.return_format_error(10002,"服务器报错！");
        }
    }

    @Override
    public Object getInvitationCode(HttpServletResponse response, HttpServletRequest request) throws Exception {
        String cookiceUser = getHeaderUser.getHader(response,request,"gg");
        String ggIdUser= JSONObject.parseObject(cookiceUser).getString("ggId");
        try {
            return return_format.return_format_All(0,ggInvitationCodeDaoMapper.getInvitationCode(ggIdUser),"");
        }catch (Exception e){
            return return_format.return_format_error(10002,"服务器报错！");
        }
    }

    @Override
    public Object getInvitationUser(HttpServletResponse response, HttpServletRequest request, String invitation) throws Exception {
        try {
            return return_format.return_format_All(0,ggInvitationCodeDaoMapper.getInvitationUser(invitation).getGgIcEstablishUser(),"");
        }catch (Exception e){
            return return_format.return_format_error(10002,"服务器报错！");
        }

    }


}
